package com.stars.empoly.vmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.activity.*
import com.stars.empoly.jupsh.componnent.JpushUtils
import com.stars.empoly.model.User
import com.stars.empoly.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch


/**
 *
 *on 2020/9/23
 */
open class HomeMeVModel(application: Application) : BViewModel<BRepository>(application) {

    var user=MutableLiveData<User>()

    var  cacheSize= MutableLiveData<String>()

    var isNoMsg=MutableLiveData<Boolean>(false)



    fun getNotMsg(){
        viewModelScope.launch{
            var it=repository().lreq { NetEngine.service.notReadNumber() }
            if(it.ok()){
                var num=it.data
                isNoMsg.value = num!!>0


            }
        }
    }

    //地址管理
    fun toAddress(){
        UiSwitch.single(repository().context, AddressListActivity::class.java)

    }

    //我的关注
    fun toHeart(){
        UiSwitch.single(repository().context, HeartActivity::class.java)

    }

    //优惠卷
    fun toCoupon(){
        UiSwitch.single(repository().context, CouponActivity::class.java)

    }

    //红包
    fun toReward(){
        //todo:
       toast("敬请期待！")
    }

    //合作
    fun toCooperation(){
        UiSwitch.single(repository().context,CooperationActivity::class.java)

    }


    //消息
    fun toMsg(){
        UiSwitch.single(repository().context, MsgActivity::class.java)

    }
    //关于我们
    fun toAboutUs(){
        UiSwitch.single(repository().context, AboutActivity::class.java)


//        UiSwitch.bundle(repository().context, ArticleDetailActivity::class.java,B().putString("type","1").ok())

    }

    //设置
    fun toSetting(){
        UiSwitch.single(repository().context, SettingActivity::class.java)

    }

    //意见反馈
    fun toOpin(){

        UiSwitch.single(repository().context, OpinionActivity::class.java)

    }

    //余额--提现
    fun toBalance(){

//        UiSwitch.single(repository().context,  SurplusActivity::class.java)


    }
    //充值
    fun toRecharge(){
        UiSwitch.single(repository().context, BalanceActivity::class.java)
    }

    fun getUserInfo(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.getUser() }
            if(it.ok()){
                user.value=it.data

                //设置别名
                JpushUtils.setPushAlias(repository().context,it.data?.id!!)
                //保存
                MMKV.defaultMMKV().putString("userId", it.data?.id)

                view().refreshing(false)
            }else{
                view().refreshing(false)
            }
        }
    }


}
